﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Web.UI.HtmlControls;
using System.IO;

namespace WebSys.UI.Control.ZhengShou
{
    public class FileUpload6Control : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                BindData();
                QSDataDLL();
            }
            Bind();
            DropDownList DropDownList1 = (DropDownList)FindControl("DropDownList1");
            string temp = HttpContext.Current.Request["usetype"];
            string cid = HttpContext.Current.Request["cid"];
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            CustomGridView1.OnLoadData += new EventHandler(CustomGridView1_OnLoadData);
            if (!string.IsNullOrEmpty(temp) && !string.IsNullOrEmpty(cid))
            {
                if (temp.Trim() == "101" || temp.Trim() == "1011" || temp.Trim() == "1012")
                {
                    if (_EluZSDataProvider.GetFJZT("zs_cqzhxy", "bsw", cid) == "0") {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "turnon", "<script>turnDiv();</script>");
                        CustomGridView1.AutoGenerateDeleteConfirm = false;
                    }
                }
                if (temp.Trim() == "102" || temp.Trim() == "1021" || temp.Trim() == "1022")
                {
                    if (_EluZSDataProvider.GetFJZT("zs_hbhxy", "bsw", cid) == "0")
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "turnon", "<script>turnDiv();</script>");
                        CustomGridView1.AutoGenerateDeleteConfirm = false;
                    }
                }
                if (temp.Trim() == "9")
                {
                    if (_EluZSDataProvider.GetFJZT("zs_prono", "提交状态", cid) == "1")
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "turnon", "<script>turnDiv();</script>");
                        CustomGridView1.AutoGenerateDeleteConfirm = false;
                    }
                }
                if (temp.Trim() == "6")
                {
                    if (_EluZSDataProvider.GetFJZT("zs_project", "补偿提交状态", cid) == "1")
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "turnon", "<script>turnDiv();</script>");
                        CustomGridView1.AutoGenerateDeleteConfirm = false;
                    }
                }
                DropDownList1.SelectedValue = temp.Trim();
                DropDownList1.Enabled = false;
            }
            if (!string.IsNullOrEmpty(HttpContext.Current.Request.Params["flag"]))
            {
                string flag = HttpContext.Current.Request["flag"];
                if (!string.IsNullOrEmpty(flag))
                {
                    if (flag == "show")
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "turnon", "<script>turnDiv();</script>");
                    }
                }
            }
          
        }
        private void BindData()
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            string strUseType = string.Empty;
            if (!string.IsNullOrEmpty(HttpContext.Current.Request.Params["usetype"]))
            {
                strUseType = HttpContext.Current.Request.Params["usetype"];
            }
            string strPID = string.Empty;
            if (!string.IsNullOrEmpty(HttpContext.Current.Request.Params["CID"]))
            {
                //strPID = HttpContext.Current.Request.Params["PID"];
                strPID = HttpContext.Current.Request.Params["CID"];
            }
            string itemname = string.Empty;
            if (!string.IsNullOrEmpty(HttpContext.Current.Request.Params["itemname"]))
            {
                itemname = HttpContext.Current.Request.Params["itemname"];
            }
            List<EluZSProvider.File> files = _EluZSDataProvider.GetAllFile(strPID, strUseType, itemname);
            CustomGridView1.DataSource = files;
            CustomGridView1.PageSize = 10;
            CustomGridView1.RecordCount = files.Count;
            CustomGridView1.DataKeyNames = new string[] { "ID" };
            CustomGridView1.DataBind();
            
        }
        protected void Bind()
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            Button upfile = (Button)FindControl("upfile");
            Button Button1 = (Button)FindControl("Button1");
            upfile.Click += new EventHandler(upfile_Click);
            Button1.Click += new EventHandler(button1_Click);
            CustomGridView1.RowDeleting+=new GridViewDeleteEventHandler(CustomGridView1_RowDeleting);
        }

        protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
        {
            BindData();
        }
        protected void CustomGridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            string fjid = CustomGridView1.DataKeys[e.RowIndex].Value.ToString();
            bool del = _EluZSDataProvider.DeleteXmByID("ZS_FILE", "ID", fjid);
            if (del)
            {
                BindData();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除成功！')", true);
            }
        }
        protected void upfile_Click(object sender, EventArgs e)
        {
            FileUpload picFile = (FileUpload)FindControl("picFile");
            TextBox filename = (TextBox)FindControl("filename");
            DropDownList DropDownList1 = (DropDownList)FindControl("DropDownList1");
            if (picFile.HasFile)
            {
                HttpPostedFile postfile = picFile.PostedFile;
                string fileName = Path.GetFileName(postfile.FileName);
                string fileext = fileName.Substring(fileName.LastIndexOf("."));
                if ((fileext.ToLower() == ".jpg") || (fileext.ToLower() == ".gif") || (fileext.ToLower() == ".bmp") || (fileext.ToLower() == ".jpeg")
                    || (fileext.ToLower() == ".doc") || (fileext.ToLower() == ".docx") || (fileext.ToLower() == ".xls"))
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(postfile.FileName))
                        {
                            Stream filestream = postfile.InputStream;
                            byte[] bFileContent = new byte[filestream.Length];
                            string tt = Convert.ToBase64String(bFileContent);

                            if (bFileContent.Length > 5242880)
                            {
                                HttpContext.Current.Response.Write("<script>alert('上传文件大于5M！');window.location.href=window.location.href;</script>");
                            }
                            else
                            {
                                if (bFileContent.Length > 0)
                                {
                                    filestream.Read(bFileContent, 0, bFileContent.Length);
                                    string guidstr = Guid.NewGuid().ToString().Replace("-", "").ToUpper();
                                    string filenametemp = filename.Text;
                                    if (string.IsNullOrEmpty(filenametemp))
                                    {
                                        filenametemp = fileName.Replace(fileext, "");
                                    }
                                    //_EluZSDataProvider.UpdateFileContent("ZS_FILE", guidstr, bFileContent, bFileContent.Length, HttpContext.Current.Request["PID"], filenametemp, fileext, DropDownList1.SelectedItem.Text, HttpContext.Current.Request["usetype"]);
                                    _EluZSDataProvider.UpdateFileContent("ZS_FILE", guidstr, bFileContent, bFileContent.Length, HttpContext.Current.Request["CID"], filenametemp, fileext, DropDownList1.SelectedItem.Text, HttpContext.Current.Request["usetype"]);
                                    HttpContext.Current.Response.Write("<script>alert('文件上传成功！');window.location.href=window.location.href;</script>");
                                }
                            }
                        }
                        else
                        {
                            HttpContext.Current.Response.Write("<script>alert('文件名不得为空！');history.back();</script>");
                        }
                    }
                    catch
                    {
                        HttpContext.Current.Response.Write("<script>alert('上传出错！');history.back();</script>");
                    }
                }
                else
                {
                    HttpContext.Current.Response.Write("<script>alert('上传文件只支持jpg,gif,jpeg,bmp,doc,docx,xls格式');history.back();</script>");
                }
            }
            else
            {
                HttpContext.Current.Response.Write("<script>alert('请选择上传文件！');history.back();</script>");
            }
        }
        protected void QSDataDLL()
        {
            string usetype = this.Page.Request["usetype"];
            DropDownList DropDownList1 = (DropDownList)FindControl("DropDownList1");
            List<Params> _params = _EluZSDataProvider.GetParams("附件类别");
            DropDownList1.DataSource = _params;
            DropDownList1.DataValueField = "keyvalue";
            DropDownList1.DataTextField = "keyname";
            DropDownList1.DataBind();
            
        }
        protected void button1_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Write("<script>window.parent.close();</script>");
        }
    }
}


